<?php
/**
* @file $Id: StudentTestReport.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['modfunc']!='create_image')
	DrawHeader(ProgramTitle());
$exams_RET = DBGet(DBQuery("SELECT ID,TITLE,SHORT_NAME FROM ASSESSMENT_EXAMS WHERE SYEAR='".UserSyear()."'"));
$select = '<SELECT name=score_id onchange="this.form.submit();"><OPTION value="">Please choose an exam</OPTION>';

if(count($exams_RET))
{
	foreach($exams_RET as $exam)
		$select .= '<OPTION value='.$exam['ID'].(($_REQUEST['score_id']==$exam['ID'])?' SELECTED':'').'>'.$exam['TITLE'].' ('.$exam['SHORT_NAME'].')</OPTION>';
}
$select .= '</SELECT>';

Search('student_id',$extra);

if(UserStudentID() && $_REQUEST['modfunc']!='create_image')
{
	echo "<FORM action=".PreparePHP_SELF().' method=POST>';
	DrawHeader($select,SubmitButton('Update'));
	echo '</FORM>';
}

if($_REQUEST['score_id'] && UserStudentID() && $_REQUEST['modfunc']!='create_image')
{	
	echo '<CENTER>';
	$_REQUEST['_FOCUS_PDF'] = true;
	$_REQUEST['modfunc'] = 'create_image';
	echo '<IMG SRC='.PreparePHP_SELF().'>';
	echo '</CENTER>';
	unset($_REQUEST['modfunc']);
}

if($_REQUEST['modfunc']=='create_image')
{
	require('assets/jpgraph/jpgraph.php');
	require('assets/jpgraph/jpgraph_bar.php');
	
	$sections_RET = DBGet(DBQuery("SELECT ID,TITLE,MAX_SCORE FROM ASSESSMENT_SECTIONS WHERE EXAM_ID='".$_REQUEST['score_id']."'"));
	if(count($sections_RET))
	{
		foreach($sections_RET as $section)
			$sections .= ",'".$section['ID']."'";
		$sections = substr($sections,1);
		$data_RET = DBGet(DBQuery("SELECT SCORE_ID,SCORE FROM ASSESSMENT_SCORES WHERE STUDENT_ID='".UserStudentID()."' AND SCORE_ID IN (".$sections.") AND TYPE='S'"),array(),array('SCORE_ID'));
		$max_section = $sections_RET[1]['MAX_SCORE'];
		foreach($sections_RET as $section)
		{
			$data[] = (int) $data_RET[$section['ID']][1]['SCORE'];
			$leg[] = $section['TITLE'];
			if($section['MAX_SCORE']>$max_section)
				$max_section = $section['MAX_SCORE'];
		}
	}

	$scale = $max_section;
	$multiplier = .1;
	while($scale>1)
	{
		$scale /= 10;
		$multiplier *= 10;
	}
	$scale = ceil($scale) * $multiplier - 1;
	
	$width = 100 + 100*count($sections_RET);
	if(count($sections_RET)<3)
		$width += 100;
	$graph = new Graph($width,300,"auto");
	$graph->SetScale("textlin",0,$max_section+$scale);
	
	$graph->SetMarginColor('#333366');
	$graph->img->SetMargin(50,40,30,60);
	$graph->yaxis->SetTitleMargin(35);
	$graph->legend->Pos(0.01,.99,"left","bottom");
	$graph->legend->SetFillColor('#FFFFFF');

	// Create the bar plots
	$bplot1 = new BarPlot($data);
	$bplot1->SetFillGradient("#EEEEEE","#C3C3C3",GRAD_RAISED_PANEL);
	$bplot1->SetShadow('#000000',10,5);
	$bplot1->value->Show();
	$bplot1->value->SetFont(FF_ARIAL,FS_BOLD,10);
	$bplot1->value->SetColor('#000000');
	$bplot1->value->SetFormat('%0.0f');
	$bplot1->SetAbsWidth(50);
	$bplot1->SetLegend('Student Scores');
	
	$data[1]+=60;
	$data[0]-=280;
	$data[2]+=200;
	$bplot2 = new BarPlot($data);
	$bplot2->SetFillColor("#00FF00");
	$bplot2->SetShadow('#000000',3,3);
	$bplot2->SetAbsWidth(10);
	$bplot2->SetLegend('National Average');
	
	$gb = new GroupBarPlot(array($bplot1,$bplot2));
	
	// ...and add it to the graph
	$graph->Add($gb);


	$graph->title->Set(ProgramTitle().' Chart');
	$graph->xaxis->SetTickLabels($leg);
	$graph->xaxis->SetLabelAngle(0);
	
	$graph->xaxis->title->Set("Section");
	$graph->xaxis->title->SetColor('#FFFFFF');
	$graph->yaxis->title->Set("Score");
	$graph->yaxis->title->SetColor('#FFFFFF');

	$graph->title->SetFont(FF_ARIAL,FS_BOLD);
	$graph->title->SetColor('#FFFFFF');
	$graph->yaxis->title->SetFont(FF_ARIAL);
	$graph->yaxis->SetColor('#FFFFFF');
	$graph->xaxis->title->SetFont(FF_ARIAL);
	$graph->xaxis->SetColor('#FFFFFF');

	/*
	$interval = $graph->yaxis->scale->ticks->GetMajor();
	echo $interval.' '.$scale;
	if($scale>=$interval)
		$graph->yaxis->HideLastTickLabel();	
	*/
	
	// Display the graph
	//$graph->Stroke();
	$_REQUEST['_FOCUS_PDF'] = true;
	$_GET['_FOCUS_PDF'] = true;
	$graph->Stroke();
}

?>